package com.chagee.store.operation.server.event.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chagee.store.operation.common.constant.CommonConstant;
import com.chagee.store.operation.server.entity.*;
import com.chagee.store.operation.server.event.dao.StoreConfigurationMapper;
import com.chagee.store.operation.server.model.bo.BatchUpdateSettlementBO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author : hk
 * @Date : 2024/7/2
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class StoreConfigurationBaseService extends ServiceImpl<StoreConfigurationMapper, StoreConfigurationDO> {

	@Resource
	private StoreConfigurationMapper storeConfigurationMapper;

	/**
	 * 通过门店编码查询信息
	 */
	public List<StoreConfigurationDO> getByStoreCode(String storeCode) {
		return storeConfigurationMapper.getByStoreCode(storeCode);
	}

	/**
	 * 更新
	 */
	public Boolean updateStoreConfiguration(BatchUpdateSettlementBO settlementBO) {
		return this.update(
			lambdaUpdate().eq(StoreConfigurationDO::getStoreCode, settlementBO.getStoreNo())
				.set(ObjectUtil.isNotEmpty(settlementBO.getFloorPrice()), StoreConfigurationDO::getFloorPrice,settlementBO.getFloorPrice())
				.set(StoreConfigurationDO::getUpdateTime, new Date())
				.getWrapper());
	}

	public List<StoreConfigurationDO> batchGetByStoreCodeList(List<String> storeCodeList) {
		LambdaQueryWrapper<StoreConfigurationDO> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper
			.in(StoreConfigurationDO::getStoreCode, storeCodeList)
			.eq(StoreConfigurationDO::getDeleteFlag, NumberUtils.INTEGER_ZERO);
		return storeConfigurationMapper.selectList(queryWrapper);
	}

	public StoreConfigurationDO queryStoreConfig(String storeCode) {
		LambdaQueryWrapper<StoreConfigurationDO> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(StringUtils.isNotBlank(storeCode), StoreConfigurationDO::getStoreCode, storeCode);
		wrapper.eq(StoreConfigurationDO::getDeleteFlag, CommonConstant.NOT_DELETE_FLAG);
		wrapper.last(CommonConstant.LIMIT_ONE_SQL);
		return baseMapper.selectOne(wrapper);
	}

}
